﻿using ConfigInfoWebPlatform.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace ConfigInfoWebPlatform.API.DBCommon
{
    /// <summary>
    /// 菜单统计
    /// </summary>
    public class DAL_MenuStatistics
    {
        /// <summary>
        /// 配置文件数据层
        /// </summary>
        public DAL_MenuStatistics()
        {
            freeSql = FreesqlFactory.GetFreeSql("configplatform");
        }
        private IFreeSql freeSql;

        /// <summary>
        /// 根据条件获取菜单统计数据
        /// </summary>
        public List<MenuStatistics> GetMenuStatisticsByFilter(Expression<Func<MenuStatistics,bool>>where)
        {
            var query = freeSql.Select<MenuStatistics>()
                             .WhereIf(where != null, where)
                             .ToList();
            return query;
        }
        
        /// <summary>
        /// 添加菜单统计信息
        /// </summary>
        public bool InsertsMenuStatistics(MenuStatistics model)
        {
            var result = freeSql.Insert<MenuStatistics>(model).ExecuteAffrows();
            return result > 0;
        }
        
        /// <summary>
        /// 根据菜单Id获取菜单统计信息
        /// </summary>
        public MenuStatistics GetStatisticsByNavId(long navId)
        {
            var query = freeSql.Select<MenuStatistics>()
                             .Where(x => x.NavicatId == navId)
                             .ToOne();
            return query;
        }

        /// <summary>
        /// 修改菜单对应的文件数量
        /// </summary>
        public bool UpdateStatisticsFileNum(long navId,int fileNum)
        {
            var result = freeSql.Update<MenuStatistics>()
                              .Set(x => x.FileNumber, fileNum)
                              .Where(x => x.NavicatId == navId)
                              .ExecuteAffrows();
            return result > 0;
        }


        /// <summary>
        /// 修改菜单对应的文件夹数量
        /// </summary>
        public bool UpdateStatisticsFolderNum(long navId, int floderNum)
        {
            var result = freeSql.Update<MenuStatistics>()
                              .Set(x => x.FloderNumber, floderNum)
                              .Where(x => x.NavicatId == navId)
                              .ExecuteAffrows();
            return result > 0;
        }

        /// <summary>
        /// 菜单文件数量递增
        /// </summary>
        public bool FileNumIncremental(long navId)
        {
            var result = freeSql.Update<MenuStatistics>()
                              .Set(x => x.FileNumber+1)
                              .Where(x => x.NavicatId == navId)
                              .ExecuteAffrows();
            return result > 0;
        }

        /// <summary>
        /// 菜单文件数量递增
        /// </summary>
        public bool FileNumDecrement(long navId)
        {
            var result = freeSql.Update<MenuStatistics>()
                              .Set(x => x.FileNumber - 1)
                              .Where(x=>x.FileNumber>0)
                              .Where(x => x.NavicatId == navId)
                              .ExecuteAffrows();
            return result > 0;
        }

        /// <summary>
        /// 菜单文件夹数量递增
        /// </summary>
        public bool FloderNumIncremental(long navId)
        {
            var result = freeSql.Update<MenuStatistics>()
                              .Set(x => x.FloderNumber + 1)
                              .Where(x => x.NavicatId == navId)
                              .ExecuteAffrows();
            return result > 0;
        }

        /// <summary>
        /// 菜单文件夹数量递减
        /// </summary>
        public bool FloderNumDecrement(long navId)
        {
            var result = freeSql.Update<MenuStatistics>()
                              .Set(x => x.FloderNumber - 1)
                              .Where(x => x.FloderNumber > 0)
                              .Where(x => x.NavicatId == navId)
                              .ExecuteAffrows();
            return result > 0;
        }
    }
}
